package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.example.springboot.common.Constants;
import com.example.springboot.common.Result;
import com.example.springboot.controller.dto.UserDTO;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.Usermapper;
//import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//用户控制器
@RestController
//查询所有用户
//@RequestMapping("/user")
public class UserController {

    @Autowired
    private Usermapper usermapper;

//    @Autowired
//    private UserService userService;
    @PostMapping("/login")
    public boolean login(@RequestBody User user) {
        String username = user.getUsername();
//        System.out.print(username);
        String password = user.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return false;
        }
        String a = usermapper.getPasswordByUsername(username);
        System.out.print(a);
        System.out.print(password);
            if (a.equals(password)){
                return true;
            }else {
                return false;
            }
        }

    @PostMapping("/register")
    public boolean register(@RequestBody User userDTO) {
        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return false;
        }else {
            usermapper.insert(userDTO);
            return true;
        }

    }

//插入语句，即注册
//    @PostMapping
//    public Integer save(@RequestBody User user)
//    {
//        return usermapper.insert(user);
//    }

    //chaxunsuoyou
    @GetMapping("/")
    public List<User> index(){
        List<User> all = usermapper.finAll();
        return all;

    }
}
